<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
use think\Controller;

class Product extends Base
{
    public function index()
    {

        $data = input('param.');

        $query = http_build_query($data);
//        halt($data);

        $whereData = [];
        // 转换查询条件
//        if(!empty($data['start_time']) && !empty($data['end_time'])
//            && $data['end_time'] > $data['start_time']
//        ) {
//            $whereData['create_time'] = [
//                ['gt', strtotime($data['start_time'])],
//                ['lt', strtotime($data['end_time'])],
//            ];
//        }

        if(!empty($data['name'])) {
            $whereData['name'] = ['like', '%'.$data['name'].'%'];
        }
        if(!empty($data['fid'])){
            $whereData['fid'] =  intval($data['fid']);;
        }




        $this->getPageAndSize($data);

        // 获取表里面的数据
        $products = model('Products')->getProductsByCondition($whereData, $this->from, $this->size);
        // 获取满足条件的数据总数 =》 有多少页

        $total = model('Products')->getProductsCountByCondition($whereData);
        /// 结合总数+size  =》 有多少页
        $pageTotal = ceil($total/$this->size);//1.1 =>2

        //  return var_dump($productResult);

        $place=model('Place')->select();
        $mater=model('Mater')->select();
        $fishing=model('Fishing')->select();
        $tail=model('Tail')->select();

        return $this->fetch('',['products'=> $products,
            'pageTotal' => $pageTotal,
            'total'=>$total,
            'curr' => $this->page,
            'query' => $query,
            'place'=>$place,
            'mater'=>$mater,
            'fishing'=>$fishing,
            'tail'=>$tail
        ]);
    }
        public function add()
        {
            $place=model('Place')->select();
            $mater=model('Mater')->select();
            $fishing=model('Fishing')->select();
            $tail=model('Tail')->select();
            return $this->fetch('', ['place'=> $place,
            'mater'=>$mater,
            'fishing'=>$fishing,
            'tail'=>$tail]);
        }

        public function save()
        {
            if(request()->isPost()) {

                $data = input('post.');

                $type =$_FILES['face']['type'];
                $file = request()->file('face');
                if (empty($file)) {
                    //必须上传商品logo图片
                    $this->fail('请上传文件');
                }
                //将文件移动到指定的目录（public 目录下  uploads目录 goods目录）
                $dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $type;
                if(!is_dir($dir)) mkdir($dir,0777,true);;
                $info = $file->validate(['size' => 10*1024*1024, 'ext' => ['jpg', 'png', 'gif', 'jpeg']])->move($dir);
                if (empty($info)) {
                    //上传出错
                    $msg = $file->getError();
                    $this->fail($msg);
                }
                //拼接图片的访问路径
                $logo = DS . "uploads" . DS . $type . DS . $info->getSaveName();

                $insertData=[
                    'name'=>$data['name'],
                    'pid'=>$data['pid'],
                    'mid'=>$data['mid'],
                    'fid'=>$data['fid'],
                    'tid'=>$data['tid'],
                    'price'=>$data['price'],
                    'stock'=>$data['stock'],
                    'parameter'=>$data['parameter'],
                    'headimg'=>$logo,
                    'detail'=>$data['editorValue'],
                    'displayorder'=>$data['displayorder']
                ];

                //入库操作
                    $id = model('Products')->create($insertData, true);;

                if($id) {
                    $this->success('新增成功', 'index');
                } else {
                    $this->error('新增失败', 'index');
                }

            }
        }
        public function del($id)
        {
            $goods =model('Products')::find($id);
            $goods->delete();


            $this->success('删除成功', 'index');
        }
        public function stop($id)
        {
            $goods =model('Products')::find($id);
            if($goods['status']==1){
                Db::table('fx_products')
                    ->where('id', $id)
                    ->update(['status' => 0]);
                $this->success('停用成功', 'index');
            }else{
                Db::table('fx_products')
                    ->where('id', $id)
                    ->update(['status' => 1]);
                $this->success('启用成功', 'index');
            }




        }
    public function edit($id)
    {
        $goods =model('Products')::find($id);
        $place=model('Place')->select();
        $mater=model('Mater')->select();
        $fishing=model('Fishing')->select();
        $tail=model('Tail')->select();


        return $this->fetch('edit',['goods'=>$goods,
            'place'=> $place,
            'mater'=>$mater,
            'fishing'=>$fishing,
            'tail'=>$tail
            ]);
    }
    public function update()
    {
        $data = input('post.');
        if($_FILES['face']['tmp_name'])
        {
            $type =$_FILES['face']['type'];
            $file = request()->file('face');
            if (empty($file)) {
                //必须上传商品logo图片
                $this->fail('请上传文件');
            }
            //将文件移动到指定的目录（public 目录下  uploads目录 goods目录）
            $dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $type;
            if(!is_dir($dir)) mkdir($dir,0777,true);;
            $info = $file->validate(['size' => 10*1024*1024, 'ext' => ['jpg', 'png', 'gif', 'jpeg']])->move($dir);
            if (empty($info)) {
                //上传出错
                $msg = $file->getError();
                $this->fail($msg);
            }
            //拼接图片的访问路径
            $logo = DS . "uploads" . DS . $type . DS . $info->getSaveName();
            $insertData=[
                'name'=>$data['name'],
                'pid'=>$data['pid'],
                'mid'=>$data['mid'],
                'fid'=>$data['fid'],
                'tid'=>$data['tid'],
                'price'=>$data['price'],
                'stock'=>$data['stock'],
                'parameter'=>$data['parameter'],
                'headimg'=>$logo,
                'detail'=>$data['editorValue'],
                'dispalyorder'=>$data['dispalyorder']
            ];
            model('Products')::update($insertData,['id' => $data['id']], true);
        }else{
            model('Products')::update($data,['id' => $data['id']], true);
        }


        $this->success('修改成功', 'index');
    }
    public function cateadd()
    {
        if(request()->isPost()) {
                $data=input("post.");
                switch ($data['xingzhi'])
                {
                    case 1:
                        $insertData=[
                            'place'=>$data['name']
                        ];
                        $id = model('Place')->create($insertData, true);
                        break;
                    case 2:
                        $insertData=[
                            'mater'=>$data['name']
                        ];
                        $id = model('Mater')->create($insertData, true);
                        break;
                    case 3:
                        $insertData=[
                            'float'=>$data['name']
                        ];
                        $id = model('Fishing')->create($insertData, true);
                        break;
                    case 4:
                        $insertData=[
                            'tail'=>$data['name']
                        ];
                        $id = model('Tail')->create($insertData, true);
                        break;
                }

            if($id) {
                $this->success('新增成功', 'admin/index/category_manage');
            } else {
                $this->error('新增失败', 'admin/index/category_manage');
            }

        }
    }
    public function catedel($id,$xingzhi)
    {
        switch ($xingzhi)
        {
            case 1:
                $cate =model('Place')::find($id);
                $cate->delete();
                break;
            case 2:
                $cate =model('Mater')::find($id);
                $cate->delete();
                break;
            case 3:
                $cate =model('Fishing')::find($id);
                $cate->delete();
                break;
            case 4:
                $cate =model('Tail')::find($id);
                $cate->delete();
                break;
        }

        $this->success('删除成功', 'admin/index/category_manage');
    }
    public function cateedt($id,$xingzhi)
    {

        switch ($xingzhi)
        {
            case 1:
                $place=model('Place')::find($id);
              return $this->fetch('',['place'=>$place,
                  'xingzhi'=>1
                  ]);


            case 2:
             $mater =model('Mater')::find($id);
               $mater= $mater["mater"];

               return $this->fetch('cateedt2',['mater'=>$mater,
                   'mid'=> $id
                   ]);

            case 3:
                $fishing =model('Fishing')::find($id);
                $float= $fishing["float"];
                return $this->fetch('cateedt3',['float'=>$float,
                    'fid'=> $id
                ]);

            case 4:
                $tail =model('Tail')::find($id);
                $tail= $tail["tail"];
                return $this->fetch('cateedt4',['tail'=>$tail,
                    'tid'=> $id
                ]);


        }
    }
    public function cateupd()
    {
        $data=input('post.');
        switch ($data['xingzhi'])
        {
            case 1:
                Db::table('fx_place')
                    ->where('pid', $data['id'])
                    ->update(['place' => $data['name']]);
                return $this->success('修改成功','admin/index/category_manage');
            case 2:
                Db::table('fx_mater')
                    ->where('mid', $data['id'])
                    ->update(['mater' => $data['name']]);
                return $this->success('修改成功','admin/index/category_manage');
            case 3:
                Db::table('fx_fishing')
                    ->where('fid', $data['id'])
                    ->update(['float' => $data['name']]);
                return $this->success('修改成功','admin/index/category_manage');
            case 4:
                Db::table('fx_tail')
                    ->where('tid', $data['id'])
                    ->update(['tail' => $data['name']]);
                return $this->success('修改成功','admin/index/category_manage');
        }
    }
}